package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Brand;
import com.cskaoyan.mall.bean.Issue;
import com.cskaoyan.mall.bean.bo.IssueListBO;
import com.cskaoyan.mall.bean.vo.GeneralItemsBean;
import com.cskaoyan.mall.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: dyj
 * @Date: 2021/1/10 23:57
 * @Description
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public GeneralItemsBean selectIssueList(IssueListBO issueListBO) {
        if(issueListBO.getQuestion() != null){
            issueListBO.setQuestion("%" + issueListBO.getQuestion().trim() + "%");
        }

        // 分页处理
        PageHelper.startPage(issueListBO.getPage(),issueListBO.getLimit());

        // 查询
        List<Issue> issueList = issueMapper.selectIssueList(issueListBO);

        // 获取总数目
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issueList);

        int total = (int) issuePageInfo.getTotal();

        return GeneralItemsBean.data(issueList,total);
    }

    @Override
    public void createIssue(Issue issue) {
        // 设置添加时间和更新时间
        Date date = new Date();
        String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        issue.setAddTime(nowtime);
        issue.setUpdateTime(nowtime);

        // 插入 使用useGeneratedKeys
        issueMapper.insertSelective(issue);
    }

    @Override
    public void updateIssueById(Issue issue) {
        // 更新时间
        String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        issue.setUpdateTime(nowtime);
        issueMapper.updateByPrimaryKeySelective(issue);
    }

    @Override
    public void deleteIssue(Integer id) {
        issueMapper.deleteByPrimaryKey(id);
    }
}
